package utils;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import beans.DataStatistics;
import beans.Package;

import jxl.Workbook;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;


public class MakeExcelHelper {

	public static void MakeExcelByPackage(List<Package> list,HttpServletResponse response){
		 // 创建工作表
		  WritableWorkbook book=null;
		  response.reset();
		  // 创建工作流
		  OutputStream os =null;
		  try {
		   // 设置弹出对话框
		   response.setContentType("application/DOWLOAD");
		   // 设置工作表的标题
//		   String local = list.get(0).getTerminal().getBranch().getBranchName()+list.get(0).getTerminal().getName();
		   response.setHeader("Content-Disposition",
		     "attachment; filename=data.xls");//设置生成的文件名字
		   os  = response.getOutputStream();
		   
		   // 初始化工作表
		   book = Workbook.createWorkbook(os);
		 
		  } catch (IOException e1) {
		   // TODO Auto-generated catch block
		   e1.printStackTrace();
		  }
		  try{
			  //以下是我做的导出日志的一个模版
		   WritableSheet sheet = book.createSheet("剩余包裹", 0);
		   // 生成名为"商品信息"的工作表，参数0表示这是第一页
		   // 表字段名
		   sheet.addCell(new jxl.write.Label(0, 0, "设备网点"));
		   sheet.addCell(new jxl.write.Label(1, 0, "设备名称"));
		   sheet.addCell(new jxl.write.Label(2, 0, "柜号"));
		   sheet.addCell(new jxl.write.Label(3, 0, "用户手机"));
		   sheet.addCell(new jxl.write.Label(4, 0, "运单号"));
		   sheet.addCell(new jxl.write.Label(5, 0, "存入时间"));
		   sheet.addCell(new jxl.write.Label(6, 0, "滞留天数"));
		   sheet.addCell(new jxl.write.Label(7, 0, "校验码"));
		   sheet.addCell(new jxl.write.Label(8, 0, "短信情况"));
		   sheet.addCell(new jxl.write.Label(9, 0, "状态"));
		   // 将数据追加
		   int temp = 0;
		   for(int i=0;i<list.size();i++){
		     sheet.addCell(new jxl.write.Label(0, i+1, list.get(i).getTerminal().getBranch().getBranchName()));
		     sheet.addCell(new jxl.write.Label(1, i+1, list.get(i).getTerminal().getName()));
		     sheet.addCell(new jxl.write.Label(2, i+1, String.valueOf(list.get(i).getBoxId())));
		     sheet.addCell(new jxl.write.Label(3, i+1, list.get(i).getUserPhone()));
		     sheet.addCell(new jxl.write.Label(4, i+1, list.get(i).getParcelId()));
		     sheet.addCell(new jxl.write.Label(5, i+1, list.get(i).getStoreTime().toString().substring(0, 19)));
		     sheet.addCell(new jxl.write.Label(6, i+1, String.valueOf(list.get(i).leftTime)));
		     sheet.addCell(new jxl.write.Label(7, i+1, list.get(i).getCheckCode()));
		     temp = list.get(i).msgStatus;
		     if(temp==-1){
		    	 sheet.addCell(new jxl.write.Label(8, i+1, "发送失败"));
		     }
		     else if(temp==1){
		    	 sheet.addCell(new jxl.write.Label(8, i+1, "发送成功"));
		     }
		     else{
		    	 sheet.addCell(new jxl.write.Label(8, i+1, "未知"));
		     }
		     temp = list.get(i).getPackageStatus();
		     if(temp==0){
		    	 sheet.addCell(new jxl.write.Label(9, i+1, "未取"));
		     }
		     else if(temp==2){
		    	 sheet.addCell(new jxl.write.Label(9, i+1, "锁定"));
		     }
		     else if(temp==4){
		    	 sheet.addCell(new jxl.write.Label(9, i+1, "退件"));
		     }else{
		    	 sheet.addCell(new jxl.write.Label(9, i+1, "未知"));
		     }
		}
		   book.write();
		   book.close();
		  } catch (Exception e) {
		   e.printStackTrace();
		  }
	}
	public static void MakeExcelByBranchData(List<DataStatistics> list,String sTime,String eTime,HttpServletResponse response){
		// 创建工作表
		WritableWorkbook book=null;
		response.reset();
		// 创建工作流
		OutputStream os =null;
		try {
			// 设置弹出对话框
			response.setContentType("application/DOWLOAD");
			// 设置工作表的标题
//		   String local = list.get(0).getTerminal().getBranch().getBranchName()+list.get(0).getTerminal().getName();
			response.setHeader("Content-Disposition",
					"attachment; filename="+sTime+"---"+eTime+".xls");//设置生成的文件名字
			os  = response.getOutputStream();
			
			// 初始化工作表
			book = Workbook.createWorkbook(os);
			
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try{
			//以下是我做的导出日志的一个模版
			WritableSheet sheet = book.createSheet("网点数据统计", 0);
			// 生成名为"商品信息"的工作表，参数0表示这是第一页
			// 表字段名
			sheet.addCell(new jxl.write.Label(0, 0, "网点地址"));
			sheet.addCell(new jxl.write.Label(1, 0, "设备数量"));
			sheet.addCell(new jxl.write.Label(2, 0, "箱格总数"));
			sheet.addCell(new jxl.write.Label(3, 0, "包裹总数"));
			sheet.addCell(new jxl.write.Label(4, 0, "滞留一天"));
			sheet.addCell(new jxl.write.Label(5, 0, "滞留三天"));
			sheet.addCell(new jxl.write.Label(6, 0, "滞留五天"));
			sheet.addCell(new jxl.write.Label(7, 0, "箱格使用率"));
			// 将数据追加
			for(int i=0;i<list.size();i++){
				sheet.addCell(new jxl.write.Label(0, i+1, list.get(i).getBranch().getBranchShortAddress()));
				sheet.addCell(new jxl.write.Label(1, i+1, list.get(i).getTerminalNum().toString()));
				sheet.addCell(new jxl.write.Label(2, i+1, list.get(i).getTotalBox().toString()));
				sheet.addCell(new jxl.write.Label(3, i+1, list.get(i).getTotalParcel().toString()));
				sheet.addCell(new jxl.write.Label(4, i+1, list.get(i).getLeft1Day().toString()));
				sheet.addCell(new jxl.write.Label(5, i+1, list.get(i).getLeft3Day().toString()));
				sheet.addCell(new jxl.write.Label(6, i+1, list.get(i).getLeft5Day().toString()));
				sheet.addCell(new jxl.write.Label(7, i+1, list.get(i).getTotalUseRate().toString()));
			}
			book.write();
			book.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
